/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper.LocationMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.param.SQLParameter;
import com.yonyou.iuap.yms.processor.AutoBeanListProcessor;
import com.yonyou.iuap.yms.processor.AutoBeanProcessor;
import com.yonyou.iuap.yms.processor.ColumnProcessor;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.Location;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.LocationEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.LocationQdEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.List;

@Repository
public class LocationMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public int getEPCNumByLocationId(String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select count(1) from dxq_coderfid a  left join dxq_location b on b.id=a.location_id  where a.location_id = ?  and b.dr=0 and a.ytenant_id = ? limit 1; ";
        parameter.addParam(id);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new ColumnProcessor(int.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public Location getLocationInfoByRfidCode(List<String> warehouseIds, String rfidCode, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where `dr`=0 ");
		if (CollectionUtil.isNotEmpty(warehouseIds)) {
            sqlBuilder.append(" and warehouse_id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < warehouseIds.size(); index++) {
				String item = warehouseIds.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < warehouseIds.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}/*else{
            sqlBuilder.append(" and warehouse_id in ('') ");
        }*/
		sqlBuilder.append(" and `rfid_code` =? and ytenant_id=? limit 1; ");
		parameter.addParam(rfidCode);
		parameter.addParam(tenant_id);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanProcessor(new Location());
        return (Location) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public List<LocationEntity> getLocationByWId(String warehouseId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where `warehouse_id` = ? and ytenant_id=? and `dr`=0; ";
        parameter.addParam(warehouseId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<Location> getLocationByWarehouseId(String warehouseId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where `warehouse_id` = ? and ytenant_id=? and dr=0 ; ";
        parameter.addParam(warehouseId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new Location());
        return (List<Location>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> getLocationByWCode(String warehouseCode, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where `warehouse_code` = ? and ytenant_id=?; ";
        parameter.addParam(warehouseCode);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> GetLocationListByPath(String locationId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where path like ( ? ) and ytenant_id=? and `dr`=0; ";
        parameter.addParam(locationId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int GetLocationNumByParentId(String locationId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select count(1) from dxq_location where parent_id = ?  and ytenant_id=? and `dr`=0; ";
        parameter.addParam(locationId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new ColumnProcessor(int.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public List<LocationEntity> selectLocationByIds(List<String> strList, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where `dr`=0 ");
		if (CollectionUtil.isNotEmpty(strList)) {
            sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}/*else{
            sqlBuilder.append(" and id in('')");
        }*/
		sqlBuilder.append(" and ytenant_id=?; ");
		parameter.addParam(tenant_id);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int updateLocationPNameByIds(String warehousename, List<String> strList, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);


//        String sql = "update dxq_location set parent_name=? where id in <foreach item='item' index='index' collection='strList' separator=',' open='(' close=')'> ? </foreach>  and tenant_id=?\n";
//        parameter.addParam(warehousename);
//        无法处理的OGNL：{item},此处正确逻辑为获取{item}值并入参
//        parameter.addParam(tenant_id);


        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("update dxq_location set parent_name=? where id in ");
        parameter.addParam(warehousename);
        if (CollectionUtil.isNotEmpty(strList)) {
            sqlBuilder.append(" ( ");
            String separator0 = " , ";
            for (int index = 0; index < strList.size(); index++) {
                String item = strList.get(index);
                sqlBuilder.append(" ? ");
                parameter.addParam(item);
                if (index < strList.size() - 1) {
                    sqlBuilder.append(separator0);
                }
            }
            sqlBuilder.append(" ) ");
        }else{
            sqlBuilder.append(" ('') ");
        }
        sqlBuilder.append(" and ytenant_id=?; ");
        parameter.addParam(tenant_id);

        String sql = sqlBuilder.toString();
        return ymsJdbcApi.update(sql, parameter);
    }

    public List<LocationEntity> getLocationEntityAll(String tenant_id) throws Exception {
        return getLocationEntityAll(tenant_id, null, null, null);
    }
    public List<LocationEntity> getLocationEntityAll(String tenant_id, String warehouse_id, String level, String locationCode) throws Exception {
        SQLParameter parameter = new SQLParameter(true);

        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select * from dxq_location where  `dr`=0 and ytenant_id=? ");
        parameter.addParam(tenant_id);

        if (warehouse_id != null && !"".equals(warehouse_id)) {
            sqlBuilder.append(" and warehouse_id = ? ");
            parameter.addParam(warehouse_id);
        }
        if (level != null && !"".equals(level)) {
            sqlBuilder.append(" and level = ? ");
            parameter.addParam(level);
        }
        if(!StringUtils.isEmpty(locationCode)){
            sqlBuilder.append(" and locationCode= ? ");
            parameter.addParam(locationCode);
        }

        String sql = sqlBuilder.toString();

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

    public List<LocationQdEntity> getLocationQdEntityAll(String tenant_id, String warehouse_id, String level, String locationCode) throws Exception {
        SQLParameter parameter = new SQLParameter(true);

        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select * from dxq_location where  `dr`=0 and ytenant_id=? ");
        parameter.addParam(tenant_id);

        if (warehouse_id != null && !"".equals(warehouse_id)) {
            sqlBuilder.append(" and warehouse_id = ? ");
            parameter.addParam(warehouse_id);
        }
        if (level != null && !"".equals(level)) {
            sqlBuilder.append(" and level = ? ");
            parameter.addParam(level);
        }
        if(!StringUtils.isEmpty(locationCode)){
            sqlBuilder.append(" and locationCode= ? ");
            parameter.addParam(locationCode);
        }

        String sql = sqlBuilder.toString();

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationQdEntity());
        return (List<LocationQdEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> GetListEx(String parentId, String level, String tenant_id, String locationCode) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where dr = 0 and ytenant_id=? ");
		parameter.addParam(tenant_id);
		if (level != null && !"".equals(level)) {
			sqlBuilder.append(" and level = ? ");
			parameter.addParam(level);
		}
		if (parentId != null && !"".equals(parentId)) {
			sqlBuilder.append(" and parent_id = ? ");
			parameter.addParam(parentId);
		}
        if(!StringUtils.isEmpty(locationCode)){
            sqlBuilder.append(" and locationCode= ? ");
            parameter.addParam(locationCode);
        }

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int updateLocationByWhCode(String warehousename, String warehousecode, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_location set `warehouse_name`=? where `warehouse_code`=? and ytenant_id=? ";
        parameter.addParam(warehousename);
		parameter.addParam(warehousecode);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int updateLocationByName(String warehousename, String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_location set `name`=? where `id`=? and ytenant_id=? ";
        parameter.addParam(warehousename);
		parameter.addParam(id);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<LocationEntity> GetLocationChildren(String locationId) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where parent_id = ? and `dr`=0; ";
        parameter.addParam(locationId);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> GetLocationAll(String tenant, String level) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where level = ? and ytenant_id = ? and `dr`=0; ";
        parameter.addParam(level);
		parameter.addParam(tenant);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> getLocationByEpc(String epccode, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location  where `rfid_code` =? and ytenant_id=? and `dr`=0; ";
        parameter.addParam(epccode);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int updateLocationForEnd(int isend, String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_location set `isEnd`=? where `id`=? and ytenant_id=? ";
        parameter.addParam(isend);
		parameter.addParam(id);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public LocationEntity getLocationById(String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where id=? and `dr`=0  and ytenant_id=? limit 1; ";
        parameter.addParam(id);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanProcessor(new LocationEntity());
        return (LocationEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int updateEntity(String key, String val, String valnew, String tenant) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_location set ? = ?  where ? = ? and ytenant_id=? ";
        parameter.addParamWithType(key, Types.VARCHAR);
		parameter.addParamWithType(valnew, Types.VARCHAR);
		parameter.addParamWithType(key, Types.VARCHAR);
		parameter.addParamWithType(val, Types.VARCHAR);
		parameter.addParamWithType(tenant, Types.VARCHAR);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<LocationEntity> getLocationByWhIds(List<String> strList, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where dr = 0 ");
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and `warehouse_id` in ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}
		sqlBuilder.append(" and ytenant_id=?; ");
		parameter.addParam(tenant_id);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> GetLocationAllByOrg(String tenant, String level, List<String> orgid) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where level = ? and ytenant_id = ? ");
		parameter.addParam(level);
		parameter.addParam(tenant);
		if (CollectionUtil.isNotEmpty(orgid)) {
			sqlBuilder.append(" and org_id in ( ");
			String separator0 = " , ";
			for (int index = 0; index < orgid.size(); index++) {
				String item = orgid.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < orgid.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
            sqlBuilder.append(" and org_id in ( '' ) ");
        }
		sqlBuilder.append(" and `dr`=0; ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<LocationEntity> GetLocationByParentId(String locationId, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where parent_id = ?  and ytenant_id=? and dr = 0; ";
        parameter.addParam(locationId);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public Location selectLocationByID(String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where id=? and `dr`=0  and ytenant_id=? limit 1; ";
        parameter.addParam(id);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanProcessor(new Location());
        return (Location) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int insertBatch(List<LocationEntity> list) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into dxq_location (`id`, `level`, `path`, `isEnd`, `name`, `locationName`, `locationCode`, `creator`, `create_time`, `warehouse_id`, `warehouse_code`, `warehouse_name`, `tenant_id`,`ytenant_id`, `org_id`, `dr`,`modifier`) values ");
		if (CollectionUtil.isNotEmpty(list)) {
			String separator0 = " , ";
			for (int i = 0; i < list.size(); i++) {
				LocationEntity item = list.get(i);
				sqlBuilder.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?) ");
				parameter.addParamWithType(item.getId(), Types.VARCHAR);
				parameter.addParamWithType(item.getLevel(), Types.INTEGER);
                parameter.addParamWithType(item.getPath(), Types.VARCHAR);
				parameter.addParamWithType(item.getIsEnd(), Types.INTEGER);
				parameter.addParamWithType(item.getName(), Types.VARCHAR);
				parameter.addParamWithType(item.getLocationName(), Types.VARCHAR);
				parameter.addParamWithType(item.getLocationCode(), Types.VARCHAR);
				parameter.addParamWithType(item.getCreator(), Types.VARCHAR);
				parameter.addParamWithType(item.getCreate_time(), Types.VARCHAR);
				parameter.addParamWithType(item.getWarehouse_id(), Types.VARCHAR);
				parameter.addParamWithType(item.getWarehouse_code(), Types.VARCHAR);
				parameter.addParamWithType(item.getWarehouse_name(), Types.VARCHAR);
				parameter.addParamWithType(item.getTenant_id(), Types.VARCHAR);
				parameter.addParamWithType(item.getTenant_id(), Types.VARCHAR);
				parameter.addParamWithType(item.getOrg_id(), Types.VARCHAR);
				parameter.addParamWithType(item.getDr(), Types.INTEGER);
				parameter.addParamWithType(item.getModifier(), Types.VARCHAR);
				if (i < list.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}
		sqlBuilder.append(" ON DUPLICATE KEY UPDATE `org_id`=VALUES(org_id),`name`=VALUES(name),`path`=VALUES(path), warehouse_code=VALUES(warehouse_code),warehouse_name=VALUES(warehouse_name), locationName=VALUES(locationName),locationCode=VALUES(locationCode),modifier=VALUES(modifier) ");

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<LocationEntity> GetLocationChildrenByOrg(String locationId, List<String> orgid) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where parent_id = ? ");
		parameter.addParam(locationId);
		if (CollectionUtil.isNotEmpty(orgid)) {
			sqlBuilder.append(" and org_id in ( ");
			String separator0 = " , ";
			for (int index = 0; index < orgid.size(); index++) {
				String item = orgid.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < orgid.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
            sqlBuilder.append(" and org_id in ( '' ) ");
        }
		sqlBuilder.append(" and `dr`=0; ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int deleteByPrimaryKey(String id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "delete from dxq_location where `id` = ? and ytenant_id=? ";
        parameter.addParam(id);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<LocationEntity> getLocationByEpcs(List<String> strList, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_location where ytenant_id=? and `dr`=0 ");
        parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and `rfid_code` in ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}
        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new LocationEntity());
        return (List<LocationEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<Location> selectLocationAll(String tenant_id) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_location where  `dr`=0 and ytenant_id=?";
        parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new Location());
        return (List<Location>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

}
